package com.seo.controller.sysUser;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.seo.function.PageTool;
import com.seo.pojo.ReturnBody;
import com.seo.service.sysRole.sdk.SysRoleService;
import com.seo.service.sysUser.sdk.SysUserService;
import com.seo.utils.ClientConst;
import com.seo.utils.Const;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户相关controller
 * 
 * @author Administrator
 * @version 2018.08.02 17:40
 */
@Slf4j
@Controller
@RequestMapping(value = "/sysUser")
public class SysUserController {

	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private PageTool pageTool;

	@Autowired
	private SysRoleService sysRoleService;

	@RequestMapping
	public String list(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpSession session) {
		log.info("进入系统用户列表...");

		PageInfo<?> page = pageTool.getZdyPage(sysUserService::getSysUserCnt, sysUserService::getSysUserList,
				params);
		request.setAttribute("page", page);

		return "sysUser/list";

	}

	@RequestMapping("/add")
	public String add(HttpServletRequest request) {
		log.info("进入添加用户页面");
		List<Map<String, Object>> roleList = sysRoleService.getAllRoleList();

		request.setAttribute("roleList", roleList);
		request.setAttribute("action", "addUser");
		return "sysUser/edit";
	}

	@RequestMapping("/addUser")
	public String addRole(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		log.info("保存用户");
		ReturnBody rb = ReturnBody.init();
		rb = sysUserService.addSysUser(params);
		request.setAttribute("result", rb.getResult());
		return "save_result";
	}

	@RequestMapping("/edit")
	public String edit(Integer userId, HttpServletRequest request) {
		log.info("进入编辑用户页面");
		List<Map<String, Object>> roleList = sysRoleService.getAllRoleList();
		Map<String, Object> sysUser = sysUserService.getSysUserById(userId);
		request.setAttribute("roleList", roleList);
		request.setAttribute("sysUser", sysUser);
		request.setAttribute("action", "editUser");
		return "sysUser/edit";
	}
	
	@RequestMapping("/editUser")
	public String editUser(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		log.info("保存用户");
		ReturnBody rb = ReturnBody.init();
		rb = sysUserService.updateSysUser(params);
		request.setAttribute("result", rb.getResult());
		return "save_result";
	}

	@RequestMapping("/deleteUser")
	@ResponseBody
	public ReturnBody deleteUser(Integer id, HttpSession session) {
		log.info("删除用户...");
		ReturnBody rb = ReturnBody.init();
		if (id == null || id <= 0) {
			return rb.error("error", "参数不能为空");
		}

		Map<String, Object> sysUser = (Map<String, Object>) session.getAttribute(Const.SESSION_USER);
		Integer userId = (Integer)sysUser.get("id");
		if(userId == id){
			return rb.error("error", "不能删除自己");
		}

		return sysUserService.deleteSysUser(id);
	}
	
	@RequestMapping("/updateState")
	@ResponseBody
	public ReturnBody updateState(Integer id,String state, HttpSession session) {
		log.info("更改用户状态...");
		ReturnBody rb = ReturnBody.init();
		if (id == null || id <= 0) {
			return rb.error("error", "参数不能为空");
		}

		if(!(StringUtils.equals(state, "00") || StringUtils.equals(state, "01"))){
			return rb.error("error", "状态错误");
		}

		return sysUserService.updateSysUserState(id, state);
	}
	
	@RequestMapping("/userInfo")
	public String index(HttpServletRequest request, HttpSession session) {
		log.info("进入客户详情...");
		Integer userId = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);
		Map<String, Object> sysUser = sysUserService.getSysUserById(userId);

		request.setAttribute("sysUser", sysUser);

		return "sysUser/userInfo";

	}
	
	@RequestMapping("/userInfo/save")
	@ResponseBody
	public ReturnBody editUserInfo(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		log.info("保存用户");
		ReturnBody rb = ReturnBody.init();
		String id = (String) params.get("id");
		if (!StringUtils.isNumeric(id)) {
			log.error("用户ID不是数字");
			return rb.error("用户不存在");
		}

		Integer curUserId = Jurisdiction.getT("id", Integer.class);
		if (Integer.parseInt(id) != curUserId) {
			log.error("只能修改个人信息");
			return rb.error("用户不存在");
		}

		return sysUserService.updateSysUser(params);
	}
	
	@RequestMapping("/userInfo/toUpdatePwd")
	public String toUpdatePwd(){
		return "sysUser/updatePwd";
	}
	
	@RequestMapping("/userInfo/updatePwd")
	@ResponseBody
	public ReturnBody updatePwd(@RequestParam Map<String, Object> params, HttpSession session) {
		log.info("修改用户密码...");

		Integer id = Jurisdiction.getT("id", Integer.class);
		params.put("id", id);

		return sysUserService.updateUserPwd(params);
	}
}
